// i.r(t), i.d(t, { default: () => RoomDataModule })
import Module from "./Module"
import MeshData from "../data/MeshData"
import RoomData from "../data/RoomData"
import SweepData from "../data/SweepData"
import ViewmodeData from "../data/ViewmodeData"
import { RoomVisitedMessage } from "../message/room.message"

export default class RoomDataModule extends Module {
  visitedRooms: any
  data: RoomData
  constructor() {
    super(...arguments), (this.name = "room-data"), (this.visitedRooms = {})
  }
  async init(e, t) {
    const [i, n] = await Promise.all([t.market.waitForData(MeshData), t.market.waitForData(ViewmodeData)])
    this.configureStorage(e)
    const c: any = {}
    this.data = new RoomData(c || {})
    const h = this.data.mapMeshesToRooms(i.meshGroups.rooms)
    i.setMeshIds({ roomIdMap: h }), t.market.register(this, RoomData, this.data)
    const d = this.data.roomCount,
      u = e => {
        const i = n.closestMode
        this.visitedRooms[e.id] || (this.visitedRooms[e.id] = { visitCount: 0 }), this.visitedRooms[e.id].visitCount++
        const s = this.visitedRooms[e.id].visitCount
        t.broadcast(new RoomVisitedMessage(e.id, e.meshSubgroup, i, s, d, ""))
      },
      p = await t.market.waitForData(SweepData),
      m = p.makeSweepChangeSubscription(e => {
        if (e) {
          const t = p.getSweep(e)
          ;(this.data.selected.value = t.roomId), this.data.commit()
        }
      }),
      g = this.data.selected.onChanged(e => {
        if (null !== e) {
          const t = this.data.get(e)
          t && u(t)
        }
      })
    this.bindings.push(m, g)
  }
  configureStorage(e) {}
}
